package com.liyu.iblog.web;

import java.util.List;

import org.mcavallo.opencloud.Cloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.liyu.iblog.domain.Category;
import com.liyu.iblog.domain.Comment;
import com.liyu.iblog.domain.Tag;
import com.liyu.iblog.service.CategoryService;
import com.liyu.iblog.service.CommentService;
import com.liyu.iblog.service.TagService;
import com.liyu.iblog.utils.TagCloudUtils;

@Controller
@RequestMapping("/ajax")
public class AsynchronousController {

    private CommentService commentService;
    private CategoryService categoryService;
    private TagService tagService;
    
    @Autowired
    public AsynchronousController(CommentService commentService, CategoryService categoryService, TagService tagService) {
        this.commentService = commentService;
        this.categoryService = categoryService;
        this.tagService = tagService;
    }
    
    @RequestMapping(value = "/blog-siderbar", method = RequestMethod.GET)
    public String loadBlogSiderBar(Model model) {
        List<Category> categories = categoryService.getAllCategories();
        model.addAttribute("categoryList", categories);
        List<Tag> tags = tagService.getAllTags();
        Cloud cloud = TagCloudUtils.generateTagCloud(tags);
        model.addAttribute("tagList", cloud.tags());
        return "ajax/blogSiderbar";
    }
    
    @RequestMapping(value = "/comment-list/{blogId}", method = RequestMethod.GET)
    public String loadCommentList(
            @PathVariable String blogId,
            Model model) {
        List<Comment> commentList = commentService.getCommentsByBlogId(blogId);
        model.addAttribute("comments", commentList);
        return "ajax/blogCommentList";
    }
}
